{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "__file__ = 'baseline-Xception'\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "from glob import glob\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import keras\n",
    "from keras.applications.inception_v3 import InceptionV3\n",
    "from keras.applications.xception import Xception\n",
    "from keras.applications.resnet50 import ResNet50\n",
    "from keras_applications.resnext import ResNeXt50\n",
    "from keras.applications.nasnet import NASNetLarge\n",
    "from keras.applications.inception_resnet_v2 import InceptionResNetV2\n",
    "from keras.models import *\n",
    "from keras.layers import *\n",
    "from keras.optimizers import *\n",
    "from keras.callbacks import *\n",
    "import PIL\n",
    "import time\n",
    "\n",
    "def preprocess_img(x):\n",
    "    x = x / 127.5\n",
    "    x -= 1.\n",
    "    return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "path_data = '../garbage_classify/train_data'\n",
    "batch_size = 32\n",
    "img_size = 299\n",
    "img_width = img_size\n",
    "img_height = img_size\n",
    "random_seed = 201908\n",
    "path_data_train = '../tmp/data_train/'\n",
    "path_data_valid = '../tmp/data_valid/'\n",
    "labels_file = '../tmp/labels_raw.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7f3daeacfb38>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEGCAYAAACJnEVTAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAATcElEQVR4nO3df5Bd5X3f8fcHIaRSCNYvGIIgghZTgcEUVgTHPyJnXJBwO0CGcUlJrYl/EBwntTNTxqTjCbE9dIynSRPZAVutKWRqQzC2sVpTI0EBN40BixRj8VMqhtESQDKyMcUGS/DtH/csvVlWP3ZX0r3q837N7Nxzn/Occ7/7jPS5Z59z7rmpKiRJbThg0AVIkvYdQ1+SGmLoS1JDDH1JaoihL0kNOXDQBezM/Pnza9GiRYMuQ5L2K/fdd98Pq2rBROuGOvQXLVrEunXrBl2GJO1Xkjy5o3VO70hSQwx9SWqIoS9JDRnqOX1JGm/btm2Mjo7y0ksvDbqUgZs9ezYLFy5k5syZu72NoS9pvzI6Osqhhx7KokWLSDLocgamqnjuuecYHR3l2GOP3e3tnN6RtF956aWXmDdvXtOBD5CEefPmTfovHkNf0n6n9cAfM5VxMPQlqSGGviRNwcqVK1m8eDEXXXTRoEuZFE/kStIUXHXVVdx2220sXLhw0KVMikf6kjRJl1xyCY8//jjLly/nsMMO433vex9Lly7luOOOY+XKla/1O++88zj99NM56aSTWLVq1WvthxxyCJdeeiknnXQS73rXu7j33ntf23716tUAvPLKK1x66aUsWbKEU045hS984Qt7pPYM89cljoyMlPfekdTv4YcfZvHixQB84r88yEN/+5M9uv8Tf/EXuPyfnbTLfmP3Bvvc5z7HmjVruOOOO3jhhRc44YQTeOaZZ5g5cyZbt25l7ty5/OxnP2PJkiXcddddr115dMstt7B8+XLOP/98XnzxRb75zW/y0EMPsWLFCu6//35WrVrF5s2b+fj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ktQQQ1+SGmLoS1JDDH2pT5KdXl6XZFH/XUB3c5/XJrlg1z2lvc/Ql6SGGPrSBJIckuT2JH/TfVdC/11eD0zypSQPJ7kpycHdNqcnuau78d6t4z71Kg0FQ1+a2EvA+d2N9N4J/HH30XqAE4Crqmox8BPgd7r723yW3r3UTweuAYbmk9nSmAN33UVqUoB/290x9VV6t/Mdu03ypqr6n93yfwb+FfAt4E3A2u69YQa9u1dKQ8XQlyZ2Eb17qJxeVduSPAHM7taNv3dJ0XuTeLCq3rLvSpQmz+kdaWKHAZu7wH8n8Et9645JMhbu/wL4K+BRYMFYe5KZSU7apxVLu8HQlyb2JWAkyfeB9wKP9K17lN6X5TwMzAGu7r7W8wLgyiTfo3eHxV/ZxzVLu+RdNiWpIR7pS1JDDH1JaoihL0kNMfQlqSGGviQ1xNCXpIYY+pLUkP8LKWxi8D45m+sAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "labels_train = pd.read_csv('../tmp/labels_train.csv')\n",
    "labels_valid = pd.read_csv('../tmp/labels_valid.csv')\n",
    "n_classess = labels_train.label.unique().shape[0]\n",
    "labels_train.groupby(by='label').count().plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_train.label = labels_train.label.apply(lambda x: f'{x:02d}')\n",
    "labels_valid.label = labels_valid.label.apply(lambda x: f'{x:02d}')\n",
    "# labels_train['label_bin'].values = keras.utils.np_utils.to_categorical(\n",
    "#     labels_train.label, n_classess)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 40000 validated image filenames belonging to 40 classes.\n",
      "Found 2978 validated image filenames belonging to 40 classes.\n"
     ]
    }
   ],
   "source": [
    "ig = ImageDataGenerator(preprocessing_function=preprocess_img)\n",
    "\n",
    "params_g = dict(\n",
    "    batch_size=batch_size,\n",
    "    # directory=path_data,\n",
    "    # class_mode='other',\n",
    "    x_col='fname',\n",
    "    y_col='label',\n",
    "    target_size=(img_width, img_height),\n",
    "    interpolation='lanczos',\n",
    "    seed=random_seed)\n",
    "\n",
    "train_g = ig.flow_from_dataframe(\n",
    "    labels_train, path_data_train, **params_g)\n",
    "valid_g = ig.flow_from_dataframe(\n",
    "    labels_valid, path_data_valid, **params_g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
      "__________________________________________________________________________________________________\n",
      "Layer (type)                    Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      "input_1 (InputLayer)            (None, 299, 299, 3)  0                                            \n",
      "__________________________________________________________________________________________________\n",
      "block1_conv1 (Conv2D)           (None, 149, 149, 32) 864         input_1[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "block1_conv1_bn (BatchNormaliza (None, 149, 149, 32) 128         block1_conv1[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "block1_conv1_act (Activation)   (None, 149, 149, 32) 0           block1_conv1_bn[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block1_conv2 (Conv2D)           (None, 147, 147, 64) 18432       block1_conv1_act[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block1_conv2_bn (BatchNormaliza (None, 147, 147, 64) 256         block1_conv2[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "block1_conv2_act (Activation)   (None, 147, 147, 64) 0           block1_conv2_bn[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block2_sepconv1 (SeparableConv2 (None, 147, 147, 128 8768        block1_conv2_act[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block2_sepconv1_bn (BatchNormal (None, 147, 147, 128 512         block2_sepconv1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block2_sepconv2_act (Activation (None, 147, 147, 128 0           block2_sepconv1_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block2_sepconv2 (SeparableConv2 (None, 147, 147, 128 17536       block2_sepconv2_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block2_sepconv2_bn (BatchNormal (None, 147, 147, 128 512         block2_sepconv2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_1 (Conv2D)               (None, 74, 74, 128)  8192        block1_conv2_act[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block2_pool (MaxPooling2D)      (None, 74, 74, 128)  0           block2_sepconv2_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_1 (BatchNor (None, 74, 74, 128)  512         conv2d_1[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "add_1 (Add)                     (None, 74, 74, 128)  0           block2_pool[0][0]                \n",
      "                                                                 batch_normalization_1[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "block3_sepconv1_act (Activation (None, 74, 74, 128)  0           add_1[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block3_sepconv1 (SeparableConv2 (None, 74, 74, 256)  33920       block3_sepconv1_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block3_sepconv1_bn (BatchNormal (None, 74, 74, 256)  1024        block3_sepconv1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block3_sepconv2_act (Activation (None, 74, 74, 256)  0           block3_sepconv1_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block3_sepconv2 (SeparableConv2 (None, 74, 74, 256)  67840       block3_sepconv2_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block3_sepconv2_bn (BatchNormal (None, 74, 74, 256)  1024        block3_sepconv2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_2 (Conv2D)               (None, 37, 37, 256)  32768       add_1[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block3_pool (MaxPooling2D)      (None, 37, 37, 256)  0           block3_sepconv2_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_2 (BatchNor (None, 37, 37, 256)  1024        conv2d_2[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "add_2 (Add)                     (None, 37, 37, 256)  0           block3_pool[0][0]                \n",
      "                                                                 batch_normalization_2[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "block4_sepconv1_act (Activation (None, 37, 37, 256)  0           add_2[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block4_sepconv1 (SeparableConv2 (None, 37, 37, 728)  188672      block4_sepconv1_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block4_sepconv1_bn (BatchNormal (None, 37, 37, 728)  2912        block4_sepconv1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block4_sepconv2_act (Activation (None, 37, 37, 728)  0           block4_sepconv1_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block4_sepconv2 (SeparableConv2 (None, 37, 37, 728)  536536      block4_sepconv2_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block4_sepconv2_bn (BatchNormal (None, 37, 37, 728)  2912        block4_sepconv2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_3 (Conv2D)               (None, 19, 19, 728)  186368      add_2[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block4_pool (MaxPooling2D)      (None, 19, 19, 728)  0           block4_sepconv2_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_3 (BatchNor (None, 19, 19, 728)  2912        conv2d_3[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "add_3 (Add)                     (None, 19, 19, 728)  0           block4_pool[0][0]                \n",
      "                                                                 batch_normalization_3[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv1_act (Activation (None, 19, 19, 728)  0           add_3[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv1 (SeparableConv2 (None, 19, 19, 728)  536536      block5_sepconv1_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv1_bn (BatchNormal (None, 19, 19, 728)  2912        block5_sepconv1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv2_act (Activation (None, 19, 19, 728)  0           block5_sepconv1_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv2 (SeparableConv2 (None, 19, 19, 728)  536536      block5_sepconv2_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv2_bn (BatchNormal (None, 19, 19, 728)  2912        block5_sepconv2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv3_act (Activation (None, 19, 19, 728)  0           block5_sepconv2_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv3 (SeparableConv2 (None, 19, 19, 728)  536536      block5_sepconv3_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block5_sepconv3_bn (BatchNormal (None, 19, 19, 728)  2912        block5_sepconv3[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "add_4 (Add)                     (None, 19, 19, 728)  0           block5_sepconv3_bn[0][0]         \n",
      "                                                                 add_3[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv1_act (Activation (None, 19, 19, 728)  0           add_4[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv1 (SeparableConv2 (None, 19, 19, 728)  536536      block6_sepconv1_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv1_bn (BatchNormal (None, 19, 19, 728)  2912        block6_sepconv1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv2_act (Activation (None, 19, 19, 728)  0           block6_sepconv1_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv2 (SeparableConv2 (None, 19, 19, 728)  536536      block6_sepconv2_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv2_bn (BatchNormal (None, 19, 19, 728)  2912        block6_sepconv2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv3_act (Activation (None, 19, 19, 728)  0           block6_sepconv2_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv3 (SeparableConv2 (None, 19, 19, 728)  536536      block6_sepconv3_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block6_sepconv3_bn (BatchNormal (None, 19, 19, 728)  2912        block6_sepconv3[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "add_5 (Add)                     (None, 19, 19, 728)  0           block6_sepconv3_bn[0][0]         \n",
      "                                                                 add_4[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv1_act (Activation (None, 19, 19, 728)  0           add_5[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv1 (SeparableConv2 (None, 19, 19, 728)  536536      block7_sepconv1_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv1_bn (BatchNormal (None, 19, 19, 728)  2912        block7_sepconv1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv2_act (Activation (None, 19, 19, 728)  0           block7_sepconv1_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv2 (SeparableConv2 (None, 19, 19, 728)  536536      block7_sepconv2_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv2_bn (BatchNormal (None, 19, 19, 728)  2912        block7_sepconv2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv3_act (Activation (None, 19, 19, 728)  0           block7_sepconv2_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv3 (SeparableConv2 (None, 19, 19, 728)  536536      block7_sepconv3_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block7_sepconv3_bn (BatchNormal (None, 19, 19, 728)  2912        block7_sepconv3[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "add_6 (Add)                     (None, 19, 19, 728)  0           block7_sepconv3_bn[0][0]         \n",
      "                                                                 add_5[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv1_act (Activation (None, 19, 19, 728)  0           add_6[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv1 (SeparableConv2 (None, 19, 19, 728)  536536      block8_sepconv1_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv1_bn (BatchNormal (None, 19, 19, 728)  2912        block8_sepconv1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv2_act (Activation (None, 19, 19, 728)  0           block8_sepconv1_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv2 (SeparableConv2 (None, 19, 19, 728)  536536      block8_sepconv2_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv2_bn (BatchNormal (None, 19, 19, 728)  2912        block8_sepconv2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv3_act (Activation (None, 19, 19, 728)  0           block8_sepconv2_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv3 (SeparableConv2 (None, 19, 19, 728)  536536      block8_sepconv3_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block8_sepconv3_bn (BatchNormal (None, 19, 19, 728)  2912        block8_sepconv3[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "add_7 (Add)                     (None, 19, 19, 728)  0           block8_sepconv3_bn[0][0]         \n",
      "                                                                 add_6[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv1_act (Activation (None, 19, 19, 728)  0           add_7[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv1 (SeparableConv2 (None, 19, 19, 728)  536536      block9_sepconv1_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv1_bn (BatchNormal (None, 19, 19, 728)  2912        block9_sepconv1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv2_act (Activation (None, 19, 19, 728)  0           block9_sepconv1_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv2 (SeparableConv2 (None, 19, 19, 728)  536536      block9_sepconv2_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv2_bn (BatchNormal (None, 19, 19, 728)  2912        block9_sepconv2[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv3_act (Activation (None, 19, 19, 728)  0           block9_sepconv2_bn[0][0]         \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv3 (SeparableConv2 (None, 19, 19, 728)  536536      block9_sepconv3_act[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block9_sepconv3_bn (BatchNormal (None, 19, 19, 728)  2912        block9_sepconv3[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "add_8 (Add)                     (None, 19, 19, 728)  0           block9_sepconv3_bn[0][0]         \n",
      "                                                                 add_7[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv1_act (Activatio (None, 19, 19, 728)  0           add_8[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv1 (SeparableConv (None, 19, 19, 728)  536536      block10_sepconv1_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv1_bn (BatchNorma (None, 19, 19, 728)  2912        block10_sepconv1[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv2_act (Activatio (None, 19, 19, 728)  0           block10_sepconv1_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv2 (SeparableConv (None, 19, 19, 728)  536536      block10_sepconv2_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv2_bn (BatchNorma (None, 19, 19, 728)  2912        block10_sepconv2[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv3_act (Activatio (None, 19, 19, 728)  0           block10_sepconv2_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv3 (SeparableConv (None, 19, 19, 728)  536536      block10_sepconv3_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block10_sepconv3_bn (BatchNorma (None, 19, 19, 728)  2912        block10_sepconv3[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "add_9 (Add)                     (None, 19, 19, 728)  0           block10_sepconv3_bn[0][0]        \n",
      "                                                                 add_8[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv1_act (Activatio (None, 19, 19, 728)  0           add_9[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv1 (SeparableConv (None, 19, 19, 728)  536536      block11_sepconv1_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv1_bn (BatchNorma (None, 19, 19, 728)  2912        block11_sepconv1[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv2_act (Activatio (None, 19, 19, 728)  0           block11_sepconv1_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv2 (SeparableConv (None, 19, 19, 728)  536536      block11_sepconv2_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv2_bn (BatchNorma (None, 19, 19, 728)  2912        block11_sepconv2[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv3_act (Activatio (None, 19, 19, 728)  0           block11_sepconv2_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv3 (SeparableConv (None, 19, 19, 728)  536536      block11_sepconv3_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block11_sepconv3_bn (BatchNorma (None, 19, 19, 728)  2912        block11_sepconv3[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "add_10 (Add)                    (None, 19, 19, 728)  0           block11_sepconv3_bn[0][0]        \n",
      "                                                                 add_9[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv1_act (Activatio (None, 19, 19, 728)  0           add_10[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv1 (SeparableConv (None, 19, 19, 728)  536536      block12_sepconv1_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv1_bn (BatchNorma (None, 19, 19, 728)  2912        block12_sepconv1[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv2_act (Activatio (None, 19, 19, 728)  0           block12_sepconv1_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv2 (SeparableConv (None, 19, 19, 728)  536536      block12_sepconv2_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv2_bn (BatchNorma (None, 19, 19, 728)  2912        block12_sepconv2[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv3_act (Activatio (None, 19, 19, 728)  0           block12_sepconv2_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv3 (SeparableConv (None, 19, 19, 728)  536536      block12_sepconv3_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block12_sepconv3_bn (BatchNorma (None, 19, 19, 728)  2912        block12_sepconv3[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "add_11 (Add)                    (None, 19, 19, 728)  0           block12_sepconv3_bn[0][0]        \n",
      "                                                                 add_10[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "block13_sepconv1_act (Activatio (None, 19, 19, 728)  0           add_11[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "block13_sepconv1 (SeparableConv (None, 19, 19, 728)  536536      block13_sepconv1_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block13_sepconv1_bn (BatchNorma (None, 19, 19, 728)  2912        block13_sepconv1[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block13_sepconv2_act (Activatio (None, 19, 19, 728)  0           block13_sepconv1_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block13_sepconv2 (SeparableConv (None, 19, 19, 1024) 752024      block13_sepconv2_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block13_sepconv2_bn (BatchNorma (None, 19, 19, 1024) 4096        block13_sepconv2[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_4 (Conv2D)               (None, 10, 10, 1024) 745472      add_11[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "block13_pool (MaxPooling2D)     (None, 10, 10, 1024) 0           block13_sepconv2_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_4 (BatchNor (None, 10, 10, 1024) 4096        conv2d_4[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "add_12 (Add)                    (None, 10, 10, 1024) 0           block13_pool[0][0]               \n",
      "                                                                 batch_normalization_4[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "block14_sepconv1 (SeparableConv (None, 10, 10, 1536) 1582080     add_12[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "block14_sepconv1_bn (BatchNorma (None, 10, 10, 1536) 6144        block14_sepconv1[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block14_sepconv1_act (Activatio (None, 10, 10, 1536) 0           block14_sepconv1_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "block14_sepconv2 (SeparableConv (None, 10, 10, 2048) 3159552     block14_sepconv1_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "block14_sepconv2_bn (BatchNorma (None, 10, 10, 2048) 8192        block14_sepconv2[0][0]           \n",
      "__________________________________________________________________________________________________\n",
      "block14_sepconv2_act (Activatio (None, 10, 10, 2048) 0           block14_sepconv2_bn[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "global_average_pooling2d_1 (Glo (None, 2048)         0           block14_sepconv2_act[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "dense_1 (Dense)                 (None, 128)          262272      global_average_pooling2d_1[0][0] \n",
      "__________________________________________________________________________________________________\n",
      "dropout_1 (Dropout)             (None, 128)          0           dense_1[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "dense_2 (Dense)                 (None, 40)           5160        dropout_1[0][0]                  \n",
      "==================================================================================================\n",
      "Total params: 21,128,912\n",
      "Trainable params: 21,074,384\n",
      "Non-trainable params: 54,528\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "base_model = Xception(\n",
    "    weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))\n",
    "\n",
    "x = base_model.output\n",
    "x = GlobalAveragePooling2D()(x)\n",
    "x = Dense(128, activation='relu')(x)\n",
    "x=Dropout(0.3)(x)\n",
    "\n",
    "predictions = Dense(n_classess, activation='softmax')(x)\n",
    "model = Model(inputs=base_model.input, outputs=predictions)\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(f'../tmp/model_{__file__}.json','w') as f:\n",
    "    model_json = model.to_json()\n",
    "    f.write(model_json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "ckpt = ModelCheckpoint('../tmp/ckpt-'+__file__+'-Epoch_{epoch:03d}-acc_{acc:.5f}-val_acc_{val_acc:.5f}.h5', save_best_only=True, monitor='val_acc')\n",
    "\n",
    "estop = EarlyStopping(monitor='val_acc', min_delta=1e-7,verbose=1, patience=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n",
      "Epoch 1/100\n",
      "1250/1250 [==============================] - 978s 782ms/step - loss: 2.7378 - acc: 0.3657 - val_loss: 1.2319 - val_acc: 0.7692\n",
      "Epoch 2/100\n",
      "1250/1250 [==============================] - 982s 786ms/step - loss: 1.1407 - acc: 0.7267 - val_loss: 0.5993 - val_acc: 0.8534\n",
      "Epoch 3/100\n",
      "1250/1250 [==============================] - 980s 784ms/step - loss: 0.6940 - acc: 0.8196 - val_loss: 0.4637 - val_acc: 0.8727\n",
      "Epoch 4/100\n",
      "1250/1250 [==============================] - 977s 782ms/step - loss: 0.4796 - acc: 0.8738 - val_loss: 0.4011 - val_acc: 0.8866\n",
      "Epoch 5/100\n",
      "1250/1250 [==============================] - 980s 784ms/step - loss: 0.3352 - acc: 0.9128 - val_loss: 0.3951 - val_acc: 0.8897\n",
      "Epoch 6/100\n",
      "1250/1250 [==============================] - 981s 785ms/step - loss: 0.2358 - acc: 0.9402 - val_loss: 0.3733 - val_acc: 0.8951\n",
      "Epoch 7/100\n",
      "1250/1250 [==============================] - 978s 783ms/step - loss: 0.1655 - acc: 0.9587 - val_loss: 0.3710 - val_acc: 0.9005\n",
      "Epoch 8/100\n",
      "1250/1250 [==============================] - 980s 784ms/step - loss: 0.1125 - acc: 0.9741 - val_loss: 0.3962 - val_acc: 0.8887\n",
      "Epoch 9/100\n",
      "1250/1250 [==============================] - 980s 784ms/step - loss: 0.0760 - acc: 0.9842 - val_loss: 0.4012 - val_acc: 0.8958\n",
      "Epoch 10/100\n",
      "1250/1250 [==============================] - 981s 785ms/step - loss: 0.0523 - acc: 0.9890 - val_loss: 0.4059 - val_acc: 0.8965\n",
      "Epoch 11/100\n",
      "1250/1250 [==============================] - 979s 783ms/step - loss: 0.0367 - acc: 0.9931 - val_loss: 0.4009 - val_acc: 0.8992\n",
      "Epoch 12/100\n",
      "1250/1250 [==============================] - 980s 784ms/step - loss: 0.0261 - acc: 0.9956 - val_loss: 0.4196 - val_acc: 0.8982\n",
      "Epoch 13/100\n",
      "1250/1250 [==============================] - 979s 783ms/step - loss: 0.0192 - acc: 0.9968 - val_loss: 0.4412 - val_acc: 0.8982\n",
      "Epoch 14/100\n",
      "1250/1250 [==============================] - 980s 784ms/step - loss: 0.0144 - acc: 0.9977 - val_loss: 0.4415 - val_acc: 0.8995\n",
      "Epoch 15/100\n",
      "1250/1250 [==============================] - 977s 782ms/step - loss: 0.0113 - acc: 0.9985 - val_loss: 0.4758 - val_acc: 0.8995\n",
      "Epoch 16/100\n",
      "1250/1250 [==============================] - 979s 783ms/step - loss: 0.0097 - acc: 0.9982 - val_loss: 0.4859 - val_acc: 0.8955\n",
      "Epoch 17/100\n",
      "1250/1250 [==============================] - 979s 783ms/step - loss: 0.0085 - acc: 0.9985 - val_loss: 0.4778 - val_acc: 0.9029\n",
      "Epoch 18/100\n",
      "1250/1250 [==============================] - 977s 782ms/step - loss: 0.0068 - acc: 0.9989 - val_loss: 0.4680 - val_acc: 0.9050\n",
      "Epoch 19/100\n",
      "1250/1250 [==============================] - 978s 782ms/step - loss: 0.0056 - acc: 0.9990 - val_loss: 0.5197 - val_acc: 0.8934\n",
      "Epoch 20/100\n",
      "1250/1250 [==============================] - 979s 783ms/step - loss: 0.0052 - acc: 0.9991 - val_loss: 0.5161 - val_acc: 0.8975\n",
      "Epoch 21/100\n",
      "1250/1250 [==============================] - 978s 782ms/step - loss: 0.0056 - acc: 0.9988 - val_loss: 0.4808 - val_acc: 0.9019\n",
      "Epoch 22/100\n",
      "1250/1250 [==============================] - 978s 782ms/step - loss: 0.0039 - acc: 0.9994 - val_loss: 0.5139 - val_acc: 0.8988\n",
      "Epoch 23/100\n",
      "1250/1250 [==============================] - 977s 782ms/step - loss: 0.0043 - acc: 0.9992 - val_loss: 0.5179 - val_acc: 0.8951\n",
      "Epoch 24/100\n",
      "1250/1250 [==============================] - 978s 783ms/step - loss: 0.0042 - acc: 0.9994 - val_loss: 0.5140 - val_acc: 0.9002\n",
      "Epoch 25/100\n",
      "1250/1250 [==============================] - 978s 782ms/step - loss: 0.0032 - acc: 0.9994 - val_loss: 0.5602 - val_acc: 0.8961\n",
      "Epoch 26/100\n",
      "1250/1250 [==============================] - 979s 783ms/step - loss: 0.0027 - acc: 0.9996 - val_loss: 0.5674 - val_acc: 0.8961\n",
      "Epoch 27/100\n",
      "1250/1250 [==============================] - 978s 783ms/step - loss: 0.0035 - acc: 0.9992 - val_loss: 0.5640 - val_acc: 0.8917\n",
      "Epoch 28/100\n",
      "1250/1250 [==============================] - 978s 782ms/step - loss: 0.0035 - acc: 0.9995 - val_loss: 0.5009 - val_acc: 0.9094\n",
      "Epoch 29/100\n",
      "1250/1250 [==============================] - 977s 782ms/step - loss: 0.0027 - acc: 0.9993 - val_loss: 0.5970 - val_acc: 0.8934\n",
      "Epoch 30/100\n",
      "1250/1250 [==============================] - 976s 781ms/step - loss: 0.0026 - acc: 0.9995 - val_loss: 0.5956 - val_acc: 0.8958\n",
      "Epoch 31/100\n",
      "1250/1250 [==============================] - 978s 783ms/step - loss: 0.0026 - acc: 0.9994 - val_loss: 0.5534 - val_acc: 0.8988\n",
      "Epoch 32/100\n",
      "1250/1250 [==============================] - 978s 783ms/step - loss: 0.0023 - acc: 0.9995 - val_loss: 0.5345 - val_acc: 0.8992\n",
      "Epoch 33/100\n",
      "1250/1250 [==============================] - 980s 784ms/step - loss: 0.0025 - acc: 0.9995 - val_loss: 0.6067 - val_acc: 0.8978\n",
      "Epoch 34/100\n",
      "1250/1250 [==============================] - 978s 783ms/step - loss: 0.0019 - acc: 0.9996 - val_loss: 0.5655 - val_acc: 0.8995\n",
      "Epoch 35/100\n",
      "1250/1250 [==============================] - 978s 782ms/step - loss: 0.0021 - acc: 0.9995 - val_loss: 0.5560 - val_acc: 0.8999\n",
      "Epoch 36/100\n",
      "1250/1250 [==============================] - 977s 782ms/step - loss: 0.0027 - acc: 0.9994 - val_loss: 0.6361 - val_acc: 0.8934\n",
      "Epoch 37/100\n",
      "1013/1250 [=======================>......] - ETA: 2:55 - loss: 0.0018 - acc: 0.9996"
     ]
    }
   ],
   "source": [
    "model.compile(optimizer=Adam(lr=1e-5), loss='categorical_crossentropy', metrics=['accuracy'])\n",
    "\n",
    "model.fit_generator(\n",
    "    train_g,\n",
    "    # steps_per_epoch=100,\n",
    "    steps_per_epoch=train_g.n // batch_size,\n",
    "    epochs=100,\n",
    "    callbacks=[ckpt, estop],\n",
    "    validation_data=valid_g,\n",
    "    # validation_steps=1,\n",
    "    validation_steps=valid_g.n // batch_size\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
